Skip to content

feat: Create admin protected endpoint for creating users #981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dcoric
Copy link
Contributor

@dcoric dcoric commented Apr 15, 2025

Introduce a new API endpoint for creating users and a corresponding command in the git-proxy-cli to interact with this endpoint. The new functionality enables administrators to create new user accounts via the CLI.

Changes

  • Added a createUser function to packages/git-proxy-cli/index.js that handles user creation via an API call. It checks for authentication cookies and makes a POST request to the /api/auth/create-user endpoint.
  • Added a create-user command to the CLI using yargs in packages/git-proxy-cli/index.js. This command takes username, password, email, gitAccount, and an optional admin flag as arguments.
  • Created a new route /api/auth/create-user in src/service/routes/auth.js that handles the user creation request. It checks for admin privileges and validates the request body before creating the user in the database.
  • Added tests for the create-user command to packages/git-proxy-cli/test/testCli.test.js, covering scenarios such as server down, no authentication, missing required fields and successful user creation.
  • Added tests for the /api/auth/create-user route to test/testLogin.test.js to verify authentication, authorization, data validation, and successful user creation.

Impact

  • Introduces a new create-user command to the git-proxy-cli, enabling administrators to create user accounts directly from the command line.
  • The /api/auth/create-user endpoint is now available, allowing authenticated administrators to create new user accounts.
  • Depends on the axios library in packages/git-proxy-cli/index.js.
  • Relies on the database interaction functions in src/service/routes/auth.js to create user records.
  • The new feature requires the user to be authenticated as an admin to create new users.

Resolves #980

Copy link

netlify bot commented Apr 15, 2025

Deploy Preview for endearing-brigadeiros-63f9d0 canceled.

Name Link
🔨 Latest commit bfddc1c
🔍 Latest deploy log https://app.netlify.com/projects/endearing-brigadeiros-63f9d0/deploys/6891e2f2c6a496000909051e

@kriswest
Copy link
Contributor

You could replace or remove this test, which is currently skipped, with your new tests:

it('should be able to create a new user', async function () {
const res = await chai.request(app).post('/api/auth/profile').set('Cookie', `${cookie}`).send({
username: 'login-test-user',
email: '[email protected]',
gitAccount: 'test123',
admin: true,
});
res.should.have.status(200);
}).skip();

@dcoric dcoric changed the title [Feature]: Create admin protected endpoint for creating users [feat]: Create admin protected endpoint for creating users Apr 22, 2025
@dcoric dcoric changed the title [feat]: Create admin protected endpoint for creating users feat: Create admin protected endpoint for creating users Apr 22, 2025
@dcoric dcoric force-pushed the denis-coric/create-user branch 2 times, most recently from d6f580d to 271916f Compare May 16, 2025 11:02
Copy link
Contributor

@jescalada jescalada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good! I've had some problems executing the tests, although some issues were present before.

Let me know if I'm missing something! 🙂

@jescalada
Copy link
Contributor

Also, it would be very helpful to execute the git-proxy-cli tests in the CI, so we don't accidentally introduce bugs...

@JamieSlome
Copy link
Member

What's the status of this PR? @jescalada @dcoric

@dcoric
Copy link
Contributor Author

dcoric commented Jul 1, 2025

What's the status of this PR? @jescalada @dcoric

I’m currently OOO but I plan to polish this up by the end of the week. I will ping here once it is ready for another review

@dcoric dcoric force-pushed the denis-coric/create-user branch 2 times, most recently from 3f7a0be to a4d3526 Compare July 14, 2025 12:32
Copy link

codecov bot commented Jul 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.64%. Comparing base (4956b73) to head (a4d3526).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #981      +/-   ##
==========================================
+ Coverage   77.40%   77.64%   +0.23%     
==========================================
  Files          55       55              
  Lines        2293     2304      +11     
  Branches      258      258              
==========================================
+ Hits         1775     1789      +14     
+ Misses        488      485       -3     
  Partials       30       30              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dcoric
Copy link
Contributor Author

dcoric commented Jul 14, 2025

@JamieSlome @jescalada I've implemented all the requested changes. I ran into some challenges with the tests, but everything should be working correctly now. When you have a moment, could you please review the updates? It should be all set for merging.

Let me know if you spot anything else that needs adjustment - otherwise, it's good to go!

@dcoric dcoric force-pushed the denis-coric/create-user branch from a4d3526 to d4d9020 Compare August 1, 2025 13:33
Copy link
Contributor

@jescalada jescalada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far 👍🏼 I spent some time finding fixes for the failing CLI tests. Hopefully committing those directly will fix things.

@@ -483,6 +483,114 @@ describe('test git-proxy-cli', function () {
});
});

// *** create user ***

describe('test git-proxy-cli :: create-user', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions fix one of the failing CLI tests:

Suggested change
describe('test git-proxy-cli :: create-user', function () {
describe('test git-proxy-cli :: create-user', function () {
before(async function () {
await helper.addRepoToDb(TEST_REPO_CONFIG);
await helper.addUserToDb('testuser', 'testpassword', '[email protected]', 'testGitAccount1');
});
after(async function () {
await helper.removeRepoFromDb(TEST_REPO_CONFIG.name);
await helper.removeUserFromDb('newuser');
await helper.removeUserFromDb('testuser');
});

await helper.runCli(`npx -- @finos/git-proxy-cli login --username admin --password admin`);

const cli = `npx -- @finos/git-proxy-cli create-user --username newuser --email [email protected] --gitAccount newgit`;
const expectedExitCode = 4;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oddly, this fix didn't get applied previously (will fix the test):

Suggested change
const expectedExitCode = 4;
const expectedExitCode = 1;
const expectedMessages = null;
const expectedErrorMessages = ['Missing required argument: password];

try {
const cookies = JSON.parse(fs.readFileSync(GIT_PROXY_COOKIE_FILE, 'utf8'));

const response = await axios.post(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter issue here:

  351:11  error  'response' is assigned a value but never used  no-unused-vars

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: User Creation Endpoint and CLI Command [Feature]: Create admin protected endpoint for creating users
4 participants